package com.lich.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.lich.dao.AccessLogMapper;
import com.lich.entity.AccessLog;
import com.lich.entity.vo.Page;

/**
 * @description 访问记录
 * @author hgk
 *
 */
@Service
public class AccessLogService {
	
	@Resource
	private AccessLogMapper accessLogMapper;
	
	
	public void save(AccessLog accessLog){
		accessLogMapper.insert(accessLog);
	}
	
	public List<AccessLog> findByIp(String ip,Date begin,Date end){
		if (StringUtils.isBlank(ip)|begin==null|end==null) 
			return null;
		Map<String, Object> params = new HashMap<>();
		params.put("ip", ip);
		params.put("begin", begin);
		params.put("end", end);
		
		return accessLogMapper.findByIp(params);
		
	}
	/**
	 * 查询统计时间begin到end内该ip对url的访问数次
	 * @param ip
	 * @param begin
	 * @param end
	 * @return
	 */
	public long getIPCount(String ip ,Date begin, Date end,String url){
		
		if (StringUtils.isBlank(ip)|begin==null|end==null|StringUtils.isBlank(url)) 
			return 0;
		Map<String, Object> params = new HashMap<>();
		params.put("ip", ip);
		params.put("begin", begin);
		params.put("end", end);
		params.put("url", url);
		
		return accessLogMapper.getIPCount(params);
		
	}
	
	
	public Page<AccessLog> list(Page<AccessLog> page){
		int count = accessLogMapper.count();
		if(count<=0){
			page.setCount(0);
			return page;
		}
		List< AccessLog> list = accessLogMapper.list(page);
		page.setCount(count);
		page.setList(list);
		return page;
	}
	

}
